package com.diudiu.diudiu.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diudiu.diudiu.common.R;
import com.diudiu.diudiu.entity.Post;
import com.diudiu.diudiu.entity.SellChatLog;
import com.diudiu.diudiu.entity.User;
import com.diudiu.diudiu.service.PostService;
import com.diudiu.diudiu.service.SellChatLogService;
import com.diudiu.diudiu.service.UserService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/sellChat")
@CrossOrigin
public class SellChatLogController {
    @Resource
    private SellChatLogService sellChatLogService;
    @Resource
    private PostService postService;
    @Resource
    private UserService userService;

    // 购买者获取指定商品的聊天记录
    @PostMapping("/buyerGetLog")
    public R buyerGetLog(@RequestBody Map<String, Object> map) {
        Object userId = map.get("userId");
        Object itemId = map.get("itemId");
        Post post = postService.getOne(new LambdaQueryWrapper<Post>().eq(Post::getId, itemId));
        User seller = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getId, post.getUserId()));
        User Buyer = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getId, userId));
        // 获取指定商品的聊天记录
        List<SellChatLog> sellChatLogs = sellChatLogService.list(new LambdaQueryWrapper<SellChatLog>().eq(SellChatLog::getItemId, itemId).eq(SellChatLog::getBuyerId, userId));
        HashMap<String, Object> res = new HashMap<>();
        // 购买者信息
        res.put("buyer", Buyer);
        // 卖家信息
        res.put("seller", seller);
        // 聊天记录
        res.put("chatLog", sellChatLogs);
        return R.ok(res);
    }

    // 购买者发送消息
    @PostMapping("/buyerSendLog")
    public R buyerSendLog(@RequestBody Map<String, Object> map) {
        Object userId = map.get("userId");
        Object itemId = map.get("itemId");
        Object context = map.get("context");
        Integer id = Math.toIntExact(sellChatLogService.count() + 1);
        SellChatLog sellChatLog = new SellChatLog();
        sellChatLog.setId(id);
        sellChatLog.setBuyerId((Integer) userId);
        sellChatLog.setItemId((Integer) itemId);
        sellChatLog.setContext((String) context);
        sellChatLog.setSellerId(postService.getOne(new LambdaQueryWrapper<Post>().eq(Post::getId, itemId)).getUserId());
        String now = String.valueOf(System.currentTimeMillis());
        sellChatLog.setCreateTime(now);
        sellChatLogService.save(sellChatLog);
        return R.ok();
    }

    // 卖家获取指定商品的聊天记录
    @PostMapping("/sellerGetLog")
    public R sellerGetLog(@RequestBody Map<String, Object> map) {
        Object userId = map.get("userId");
        Object itemId = map.get("itemId");
        User seller = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getId, userId));
        List<SellChatLog> sellChatLogs = sellChatLogService.list(new LambdaQueryWrapper<SellChatLog>().eq(SellChatLog::getItemId, itemId).eq(SellChatLog::getSellerId, userId));
        List<User> buyers = sellChatLogs.stream().map(sellChatLog -> userService.getOne(new LambdaQueryWrapper<User>().eq(User::getId, sellChatLog.getBuyerId()))).distinct().collect(Collectors.toList());
        List<Object> chatLogs = buyers.stream().map(buyer -> {
            HashMap<String, Object> res = new HashMap<>();
            res.put("buyer", buyer);
            res.put("chatLog", sellChatLogs.stream().filter(sellChatLog -> sellChatLog.getBuyerId().equals(buyer.getId())).collect(Collectors.toList()));
            return res;
        }).collect(Collectors.toList());
        HashMap<String,Object> res = new HashMap<>();
        res.put("seller",seller);
        res.put("buyer",chatLogs);
        return R.ok(res);
    }
    // 卖家发送消息
    @PostMapping("/sellerSendLog")
    public R sellerSendLog(@RequestBody Map<String, Object> map) {
        Object userId = map.get("userId");
        Object itemId = map.get("itemId");
        Object context = map.get("context");
        Object buyerId = map.get("buyerId");
        Integer id = Math.toIntExact(sellChatLogService.count() + 1);
        SellChatLog sellChatLog = new SellChatLog();
        sellChatLog.setId(id);
        sellChatLog.setSellerId((Integer) userId);
        sellChatLog.setItemId((Integer) itemId);
        sellChatLog.setContext((String) context);
        sellChatLog.setBuyerId((Integer) buyerId);
        String now = String.valueOf(System.currentTimeMillis());
        sellChatLog.setCreateTime(now);
        sellChatLogService.save(sellChatLog);
        return R.ok();
    }

}
